﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GAE_PI.Models;
using GAE_PI.Exceptions;

namespace GAE_PI.Controllers
{
    public class ModifierActiviteController : Controller
    {
        //
        // GET: /ModifierActivite/

        public ActionResult ModifierActivite(string nomA)
        {
            try
            {
                if (!DBModel.getDBM().isAdmin())
                    return RedirectToAction("ConsulterActivites", "ConsulterActivites");
                DetailActivityModel model =  DBModel.getDBM().getDetailForActivite(nomA,true);
                Session["hashactivite"] = DBModel.getDBM().getHashDetailActivite(nomA);
                return View(model);
            }
            catch (GAEException ex)
            {
                ex.errorModel.ControllerName = "ModifierActivite";
                ex.errorModel.ControllerAction = "ModifierActivite";
                return View("Error", ex.errorModel);
            }
        }

        [HttpPost]
        public ActionResult ModifierActivite(DetailActivityModel model)
        {
            try
            {
                model.HashMD5 = (byte[])Session["hashactivite"];
                DBModel.getDBM().modifierActivite(model);
                return RedirectToAction("DetailActivite", "DetailActivite", new { nomA = model.Nom });
            }
            catch (GAEException ex)
            {
                ex.errorModel.ControllerName = "ModifierActivite";
                ex.errorModel.ControllerAction = "Valider";
                return View("Error", ex.errorModel);
            }
        }

    }
}
